//
//  IndentDetailViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/1/16.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "IndentDetailViewController.h"
#import "SureIndentTableViewCell.h"
#import "PayViewController.h"
static NSString * cellIndentifer = @"SureIndentTableViewCell";

@interface IndentDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UIView *navView;

@property (strong, nonatomic) IBOutlet UIScrollView *scroView;

@property (strong, nonatomic) IBOutlet UIView *wordsBackView;//留言椭圆背景
@property (strong, nonatomic) IBOutlet UITextView *wordsTextView;
@property (strong, nonatomic) IBOutlet UITextField *othersField;//其他
@property (strong, nonatomic) IBOutlet UIButton *dontNeedButton;// 不需要button
@property (strong, nonatomic) IBOutlet UIButton *needButton;//需要button
@property (strong, nonatomic) IBOutlet UIButton *othersButton;//其他button
@property (strong, nonatomic) IBOutlet UIView *otherChoseView;
@property (strong, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) IBOutlet UILabel *reciverName;//收货人
@property (strong, nonatomic) IBOutlet UILabel *reciverPhone;//收货人电话
@property (strong, nonatomic) IBOutlet UILabel *reciverAddress;//收货地址
@property(nonatomic,strong)NSString * storeName;

@property (strong, nonatomic) IBOutlet UILabel *totalPrice;
@property(nonatomic,assign)NSInteger selectedIndex; //发票信息选中index
@end

@implementation IndentDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectedIndex = 100;
    [self initViewLayOut];
    [self getIndentDetailInfo];
    
    // Do any additional setup after loading the view.
}

- (void)initViewLayOut{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroView];
    [self adaptiveViewLayout:self.otherChoseView];
    [self adaptiveViewLayout:self.wordsBackView];
   
    self.wordsBackView.layer.borderWidth = 1;
    self.wordsBackView.layer.borderColor = [UIColor colorWithWhite:0.906 alpha:1.000].CGColor;
    self.wordsBackView.layer.cornerRadius = 5*VerticalRatio();

    [self.reciverAddress sizeToFit];
    self.othersField.layer.cornerRadius = 15*VerticalRatio();
    self.othersField.layer.borderWidth = 1;
    self.othersField.layer.borderColor = [UIColor colorWithWhite:0.906 alpha:1.000].CGColor;
    
    [self.dontNeedButton setBackgroundImage:[UIImage imageNamed:@"sureIndent4"] forState:UIControlStateNormal];
    [self.dontNeedButton setBackgroundImage:[UIImage imageNamed:@"sureIndent3"] forState:UIControlStateSelected];
    self.dontNeedButton.selected = YES;
    UIButton *  Button = (UIButton *)[self.view viewWithTag:100];
    Button.selected = YES;
    [self.needButton setBackgroundImage:[UIImage imageNamed:@"sureIndent4"] forState:UIControlStateNormal];
    [self.needButton setBackgroundImage:[UIImage imageNamed:@"sureIndent3"] forState:UIControlStateSelected];
    [self.othersButton setBackgroundImage:[UIImage imageNamed:@"sureIndent4"] forState:UIControlStateNormal];
    [self.othersButton setBackgroundImage:[UIImage imageNamed:@"sureIndent3"] forState:UIControlStateSelected];
    
     [self.tableView registerNib:[UINib nibWithNibName:cellIndentifer bundle:nil] forCellReuseIdentifier:cellIndentifer];
}

- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}

#pragma mark====编辑地址
- (IBAction)editingAddress:(id)sender {
    
}

#pragma mark====结算
- (IBAction)payHandel:(id)sender {
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Pay"];
    [self.navigationController   pushViewController:page animated:YES];
}
#pragma mark ==== 需要发票否
- (IBAction)needOrNot:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.tag == _selectedIndex) {
        return;
    }
    UIButton * Button = (UIButton *)[self.view viewWithTag:sender.tag+4];
    Button.selected = YES;
    
    sender.selected = YES;
    UIButton * button = (UIButton *)[self.view viewWithTag:_selectedIndex];
    button.selected = NO;
    
    UIButton * buton = (UIButton *)[self.view viewWithTag:_selectedIndex+4];
    buton.selected = NO;
    
    _selectedIndex = sender.tag;
}


#pragma mark == 获取订单详情
- (void)getIndentDetailInfo{

    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    if (_type == 1) {
        [dic setObject:_goodsId forKey:@"orderId"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:SX_saveOrderDetail withHttpType:@"POST" withTag:SX_SX_saveOrderDetail];
    }else{
        [dic setObject:_goodsId forKey:@"orderId"];
        self.shareApi.delegate=self;
        [self.shareApi  requestOperationWithDic:dic withPath:SX_queryAppMallGrouponOrder withHttpType:@"POST" withTag:SX_SX_queryAppMallGrouponOrder];
    }
    
    
}



#pragma mark -- UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 102*VerticalRatio();
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35*VerticalRatio();
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

  return _goodsInfoArray.count;

}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    UIView * view = [[UIView alloc]initWithFrame:flexibleFrame(CGRectMake(0, 0, 320, 35), NO)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel * label =  [[UILabel alloc]initWithFrame:flexibleFrame(CGRectMake(12,0, 265, 38), NO)];
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = [UIColor colorWithWhite:0.196 alpha:1.000];
    label.font = [UIFont systemFontOfSize:15*VerticalTexeRatio()];
    label.text = _storeName;
    [view addSubview:label];
    
    
//    UILabel * infoLabel =  [[UILabel alloc]initWithFrame:flexibleFrame(CGRectMake(0,0, 310, 38), NO)];
//    infoLabel.textAlignment = NSTextAlignmentRight;
//    infoLabel.textColor = [UIColor colorWithWhite:0.647 alpha:1.000];
//    infoLabel.font = [UIFont systemFontOfSize:14*VerticalTexeRatio()];
//    NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:@"积分兑换:两百积分抵扣十元"];
//    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.961 green:0.635 blue:0.161 alpha:1.000] range:NSMakeRange(0,5)];
//    infoLabel.attributedText = str;
//    [view addSubview:infoLabel];
    
    return view;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SureIndentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifer];
    if (cell == nil) {
        cell= [[SureIndentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifer];
        
    }
    
    for (UIView *view in cell.subviews) {
        if ([view isKindOfClass:[UILabel class]] ||[view isKindOfClass:[UIImageView class]]||[view isKindOfClass:[UIScrollView class]]||[view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    NSDictionary * dic = _goodsInfoArray[indexPath.row];

        NSString * image = dic[@"appAdertImagUri"];
    if (_type == 2) {
        image = dic[@"mgpiProjectImg"];
    }
        if (image.length>0) {
            [cell.indentImage sd_setImageWithURL:[NSURL URLWithString:image]];
        }else{
            cell.indentImage.image =[UIImage imageNamed:@"gsInfo1"];
        }
    
        cell.indentName.frame = flexibleFrame(CGRectMake(104, 16, 204, 20), NO);
        cell.indentName.numberOfLines = 0;
        cell.indentName.text = [NSString stringWithFormat:@"%@",dic[@"mgooName"]];
    if (_type==2) {
        cell.indentName.text = [NSString stringWithFormat:@"%@",dic[@" mgpiProjectName"]];
       
    }
        [cell.indentName sizeToFit];
        
    
        cell.indentInfo.frame = flexibleFrame(CGRectMake(104, 47, 204, 20), NO);
        cell.indentInfo.numberOfLines = 0;
        cell.indentInfo.text = [NSString stringWithFormat:@"%@",dic[@"invoiceInformation"]];
    if (_type==2) {
        cell.indentInfo.text = [NSString stringWithFormat:@"%@",dic[@"mgpiProjectName"]];
        
    }
    [cell.indentInfo sizeToFit];
        cell.indentNumber.text = [NSString stringWithFormat:@"x%@",dic[@"goodsCount"]];
    if (_type==2) {
        cell.indentNumber.text = [NSString stringWithFormat:@"x%@",dic[@"mgpiProjectCount"]];
        
    }
        cell.indentPrice.text = [NSString stringWithFormat:@"￥%@",dic[@"mgooPublishPrice"]];
    if (_type==2) {
        cell.indentPrice.text = [NSString stringWithFormat:@"￥%@",dic[@"mgpiGouponAmount"]];
        
    }
    return cell;
}



#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    
    
    if (tag==SX_SX_CarCreateOrderTag) {
        
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
            PayViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Pay"];
            //            page.payInfoArray = _goodsInfoArray;
            page.payOrderName = @"凡人街在线商城";
            page.payOrderPrice = self.countMoney;
            NSArray *arr = database[@"result"];
            NSString *orderId = @"";
            for (NSDictionary *dic in arr) {
                orderId = dic[@"mgorNo"];
            }
            page.payId = orderId;
            
            [self.navigationController   pushViewController:page animated:YES];
            
        }
        
    }else if (tag == SX_SX_createPackFruitTag){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:2];
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
            PayViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Pay"];
            page.payOrderName = @"凡人街在线商城";
            page.payOrderPrice = self.countMoney;
            page.payId = database[@"result"][@"mgorNo"];
            [self.navigationController   pushViewController:page animated:YES];
            
        }
        
    }else if (tag == SX_SX_saveOrderDetail){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {

            _goodsInfoArray = database[@"result"];
            
            _reciverName.text = _goodsInfoArray[0][@"msadReceiverName"];
            _reciverPhone.text = _goodsInfoArray[0][@"phone"];
            _reciverAddress.text = _goodsInfoArray[0][@"msadReceiverAddress"];
            _storeName = _goodsInfoArray[0][@"mbraStoreName"];
            
            
            
            CGFloat money = 0;
            for (int i = 0; i<_goodsInfoArray.count; i++) {
                CGFloat price = [_goodsInfoArray[i][@"mgooPublishPrice"]floatValue];
                money += price;
            }
            NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"共%ld件,总计:￥%.2lf",_goodsInfoArray.count,money]];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithWhite:0.224 alpha:1.000] range:NSMakeRange(0,6+[NSString stringWithFormat:@"%ld",_goodsInfoArray.count].length)];
            [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15*VerticalTexeRatio()] range:NSMakeRange(0,6+[NSString stringWithFormat:@"%ld",_goodsInfoArray.count].length)];
            _totalPrice.attributedText = str;
            NSInteger count= _goodsInfoArray.count;
            _tableView.frame = flexibleFrame(CGRectMake(0, 161, 320, 35+count*102), YES);
            _otherChoseView.frame = flexibleFrame(CGRectMake(0, CGRectGetMaxY(_tableView.frame), 320, 232), YES);
            self.scroView.contentSize = flexibleSize(CGSizeMake(0,161+232+35+count*102), NO);
            _tableView.dataSource = self;
            _tableView.delegate = self;
            [_tableView reloadData];
        }
    }else if (tag == SX_SX_queryAppMallGrouponOrder){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {

            _goodsInfoArray = database[@"result"];
            
            _reciverName.text = _goodsInfoArray[0][@"nickName"];
            _reciverPhone.text = _goodsInfoArray[0][@"phone"];
//            _reciverAddress.text = _goodsInfoArray[0][@"msadReceiverAddress"];
            _storeName = _goodsInfoArray[0][@"storeName"];
            
            CGFloat money = 0;
            for (int i = 0; i<_goodsInfoArray.count; i++) {
                CGFloat price = [_goodsInfoArray[i][@"mgpiGouponAmount"]floatValue];
                money += price;
            }
            NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"共%ld件,总计:￥%.2lf",_goodsInfoArray.count,money]];
            [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithWhite:0.224 alpha:1.000] range:NSMakeRange(0,6+[NSString stringWithFormat:@"%ld",_goodsInfoArray.count].length)];
            [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15*VerticalTexeRatio()] range:NSMakeRange(0,6+[NSString stringWithFormat:@"%ld",_goodsInfoArray.count].length)];
            _totalPrice.attributedText = str;
            NSInteger count= _goodsInfoArray.count;
            _tableView.frame = flexibleFrame(CGRectMake(0, 161, 320, 35+count*102), YES);
            _otherChoseView.frame = flexibleFrame(CGRectMake(0, CGRectGetMaxY(_tableView.frame), 320, 232), YES);
            self.scroView.contentSize = flexibleSize(CGSizeMake(0,161+232+35+count*102), NO);
            _tableView.dataSource = self;
            _tableView.delegate = self;
            [_tableView reloadData];
        }
    }
    
}
-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
